package mihoyo.kqcc.globeException;


import mihoyo.result.JSONResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobeException {
    @ExceptionHandler(Exception.class)
    public JSONResult exceptionHandler(Exception e){
        e.printStackTrace();
        return JSONResult.error(GlobeExceptionEnum.GLOBE_ERROR);
    }
    @ExceptionHandler(MyGlobeException.class)
    public JSONResult GlobeExceptionHandler(MyGlobeException e){
        e.printStackTrace();
        return JSONResult.error(e);
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JSONResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        StringBuilder sb = new StringBuilder();
        e.getBindingResult().getAllErrors().forEach(err->{
            sb.append(err.getDefaultMessage()+" ");
        });
        return JSONResult.error(sb.toString(),GlobeExceptionEnum.COMMON_ERROR_IS_NULL.getEcode());
    }
}
